import { notification } from 'antd';
import { addFakeList, queryFakeList, removeFakeList, updateFakeList } from './service';

const Model = {
  namespace: 'listAndbasicList1',
  state: {
    list: {},
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      console.log(response)
      yield put({
        type: 'queryList',
        payload: response.data || {},
      });
    },

    *appendFetch({ payload }, { call, put }) {
      // 提交参数传参数给接口返回一个数据，把这个数据传到reduceer及时更新state的list数据
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'appendList',
        payload: Array.isArray(response) ? response : [],
      });
    },
    // 提交 删除或者更新 有id值
    *submit({ payload }, { call, put }) {
      console.log(payload)
      let callback;
      if (payload.id) {
        callback = Object.keys(payload).length === 1 ? removeFakeList : updateFakeList;
      } else {
        // var json = JSON.stringify(payload)
        // console.log(json)
        callback=addFakeList
      }
      const response = yield call(callback, payload); // post
      console.log(response,callback)
      // 把增加修改删除的数据后后台返回结果后也需要及时更新state的list
      // notification.info({
      //   message: response.message || '操作失败',
      // });
      // 如果操作可以返回所有数据就执行下面。否则调用查询接口
      yield put({type: 'fetch',payload:{  pageIndex: 1,
        pageSize: 20,order: "id desc"}}); //触发fetch
      // yield put({
      //   type: 'queryList',
      //   payload: response,
      // });
    },
  },
  reducers: {
    queryList(state, action) {
      console.log(action)
      return { ...state, list: action.payload };
    },

    appendList(
      state = {
        list: [],
      },
      action,
    ) {
      return { ...state, list: state.list.concat(action.payload) };
    },
  },
};
export default Model;
